package com.qianbao.qianbaobusiness.ui.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baseandroid.config.Global;
import com.baseandroid.event.ContractListEvent;
import com.baseandroid.mvp.BaseMvpActivity;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.ContractListBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ContractListActivity extends BaseMvpActivity<ContractListView, ContractListPresenter> implements ContractListView, SwipeRefreshLayout.OnRefreshListener, ContractListRvAdapter.ItemClickListen {

    @BindView(R.id.tv_title)
    TextView           mTvTitle;
    @BindView(R.id.ll_empty)
    LinearLayout       mLlEmpty;
    @BindView(R.id.rv)
    RecyclerView       mRv;
    @BindView(R.id.srl)
    SwipeRefreshLayout mSrl;
    private String                mMerchantNo;
    private String                mMerchantName;
    private ContractListRvAdapter mRvAdapter;
    private List<ContractListBean.ResultBean> mList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_contract_list;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        initIntent();
        initView();
    }

    private void initIntent() {
        Intent intent = getIntent();
        mMerchantNo = intent.getStringExtra("merchantNo") == null ? "" : intent.getStringExtra("merchantNo");
        mMerchantName = intent.getStringExtra("merchantName") == null ? "" : intent.getStringExtra("merchantName");
    }

    private void initView() {
        if (TextUtils.isEmpty(mMerchantName)) {
            mTvTitle.setText("电子合同");
        } else {
            mTvTitle.setText(mMerchantName);
        }
        mSrl.setColorSchemeColors(ContextCompat.getColor(this, R.color.text_red));
        mSrl.setOnRefreshListener(this);
        mRv.setLayoutManager(new LinearLayoutManager(this));
        if (mRvAdapter == null) {
            mRvAdapter = new ContractListRvAdapter(this);
        }
        mRv.setAdapter(mRvAdapter);
        mRvAdapter.setOnClickListen(this);
        getNetData();
    }

    private void getNetData() {
        if (TextUtils.isEmpty(mMerchantNo)) {
            getPresenter().getContractList(Global.getShopListInfo().merchantList.get(0).merchantNo);
        } else {
            getPresenter().getContractList(mMerchantNo);
        }
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick(R.id.iv_back_toolbar)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        getNetData();
    }

    @Override
    public void itemClick(int l) {
        Bundle b = new Bundle();
        b.putSerializable("list", (Serializable) mList);
        b.putInt("currentItem", l);
        Intent intent = new Intent(this, ContractDetailActivity.class);
        intent.putExtras(b);
        startActivity(intent);
    }

    @Override
    public void getContractListSuccess(List<ContractListBean.ResultBean> result) {
        if (mSrl.isRefreshing()) {
            mSrl.setRefreshing(false);
        }
        mList.clear();
        mList.addAll(result);
        if (mList.size() == 0) {
            mLlEmpty.setVisibility(View.VISIBLE);
        } else {
            mLlEmpty.setVisibility(View.INVISIBLE);
        }
        mRvAdapter.resetData(mList);
    }

    @Override
    public void onNetError() {
        if (mSrl.isRefreshing()) {
            mSrl.setRefreshing(false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ContractListEvent event) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getNetData();
            }
        }, 1000);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}


